package com.huawei.fastengine.fastview.download.download;

import android.content.Context;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.fastengine.fastview.HttpsSetting;
import com.huawei.fastengine.fastview.download.download.bean.DownloadRequest;
import com.huawei.fastengine.fastview.download.utils.HEX;
import com.huawei.fastengine.fastview.download.utils.IoUtils;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory;
import com.huawei.secure.android.common.ssl.SecureX509TrustManager;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HttpsURLConnection;
import o.ftz;
import o.fua;
import o.fus;
import o.fus.a;
import o.fuv.b;
import o.fvb;

public class DownloadManager {
    private static final String TAG = "DownloadManager";
    private static final int TIMEOUT = 80000;
    private static DownloadManager instance = new DownloadManager();
    private boolean isDownload = true;

    private boolean checkRpkValid(File file, String str) {
        byte[] digestSha2 = HashUtils.digestSha2(file);
        return digestSha2 != null ? HEX.encodeHexString(digestSha2, true).equalsIgnoreCase(str) : false;
    }

    public static boolean deleteFile(File file) {
        return (file == null || !file.exists()) ? false : file.delete();
    }

    public static DownloadManager getInstance() {
        return instance;
    }

    private static SecureSSLSocketFactory getSSLFactory(Context context) throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException, IllegalAccessException {
        return SecureSSLSocketFactory.getInstance(context);
    }

    private static SecureX509TrustManager getTrustManager(Context context) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IllegalAccessException {
        return new SecureX509TrustManager(context);
    }

    private HttpURLConnection openConn(String str, Context context) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        if (httpURLConnection instanceof HttpsURLConnection) {
            HttpsSetting.initHttpsURLConnection((HttpsURLConnection) httpURLConnection, context.getApplicationContext());
        }
        httpURLConnection.setConnectTimeout(TIMEOUT);
        httpURLConnection.setReadTimeout(TIMEOUT);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod(HttpContants.HTTP_METHOD_GET);
        return httpURLConnection;
    }

    private void setFileReadable(File file) {
        if (!file.setReadable(true, false)) {
            FastViewLogUtils.e(TAG, "can not set readable to apk");
        }
    }

    public void downloadAsync(DownloadRequest downloadRequest, Context context, DownloadListener downloadListener) {
        new Thread(new 1(this, downloadRequest, context, downloadListener)).start();
    }

    public void downloadAsyncOKHttp(final DownloadRequest downloadRequest, final Context context, final DownloadListener downloadListener) {
        try {
            b ॱ = new b().ˎ(10, TimeUnit.SECONDS).ˊ(10, TimeUnit.SECONDS).ˋ(10, TimeUnit.SECONDS).ˎ(true).ॱ(getSSLFactory(context), getTrustManager(context));
            fus ॱ2 = new a().ˎ(downloadRequest.getUrl()).ॱ();
            ॱ.ˊ(SecureSSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            ॱ.ˋ().ˎ(ॱ2).enqueue(new fua() {
                public void onFailure(ftz o_ftz, IOException iOException) {
                    downloadListener.failure(1);
                }

                public void onResponse(ftz o_ftz, fvb o_fvb) {
                    Closeable ˎ;
                    Closeable fileOutputStream;
                    Closeable closeable;
                    Throwable th;
                    Closeable closeable2 = null;
                    int i = 0;
                    if (o_fvb == null || o_fvb.ᐝ() == null) {
                        downloadListener.failure(1);
                        return;
                    }
                    int size = downloadRequest.getSize();
                    if (size == -1) {
                        try {
                            size = (int) o_fvb.ᐝ().ˏ();
                        } catch (NumberFormatException e) {
                            FastViewLogUtils.e(DownloadManager.TAG, "get download content-length failure.");
                            downloadListener.failure(1);
                            return;
                        }
                    }
                    if (o_fvb.ˊ() == ResponseBean.NOT_FOUND) {
                        downloadListener.failure(1);
                        return;
                    }
                    try {
                        File file;
                        ˎ = o_fvb.ᐝ().ˎ();
                        try {
                            file = new File(context.getFilesDir(), System.currentTimeMillis() + ".apk");
                            fileOutputStream = new FileOutputStream(file);
                        } catch (IOException e2) {
                            closeable = ˎ;
                            try {
                                FastViewLogUtils.e(DownloadManager.TAG, "IOException");
                                downloadListener.failure(1);
                                IoUtils.closeStream(closeable);
                                IoUtils.closeStream(closeable2);
                            } catch (Throwable th2) {
                                fileOutputStream = closeable2;
                                Closeable closeable3 = closeable;
                                th = th2;
                                ˎ = closeable3;
                                IoUtils.closeStream(ˎ);
                                IoUtils.closeStream(fileOutputStream);
                                throw th;
                            }
                        } catch (Throwable th3) {
                            th = th3;
                            fileOutputStream = null;
                            IoUtils.closeStream(ˎ);
                            IoUtils.closeStream(fileOutputStream);
                            throw th;
                        }
                        try {
                            byte[] bArr = new byte[2048];
                            int read = ˎ.read(bArr);
                            FastViewLogUtils.e(DownloadManager.TAG, "isDownload" + DownloadManager.this.isDownload);
                            int i2 = 0;
                            while (read != -1 && DownloadManager.this.isDownload) {
                                fileOutputStream.write(bArr, 0, read);
                                read = ˎ.read(bArr);
                                i2 += read;
                                i++;
                                if (i % 200 == 0 || i2 == size) {
                                    downloadListener.progress(i2, size);
                                }
                            }
                            if (DownloadManager.this.isDownload) {
                                downloadListener.progress(size, size);
                                if (!downloadRequest.isNeedCheck()) {
                                    DownloadManager.this.setFileReadable(file);
                                    downloadListener.success(file);
                                    IoUtils.closeStream(ˎ);
                                    IoUtils.closeStream(fileOutputStream);
                                    return;
                                } else if (DownloadManager.this.checkRpkValid(file, downloadRequest.getHash())) {
                                    DownloadManager.this.setFileReadable(file);
                                    downloadListener.success(file);
                                    IoUtils.closeStream(ˎ);
                                    IoUtils.closeStream(fileOutputStream);
                                    return;
                                } else {
                                    DownloadManager.deleteFile(file);
                                    downloadListener.failure(2);
                                    IoUtils.closeStream(ˎ);
                                    IoUtils.closeStream(fileOutputStream);
                                    return;
                                }
                            }
                            FastViewLogUtils.e(DownloadManager.TAG, "!isDownload" + DownloadManager.this.isDownload);
                            DownloadManager.deleteFile(file);
                            downloadListener.failure(4);
                            DownloadManager.this.isDownload = true;
                            IoUtils.closeStream(ˎ);
                            IoUtils.closeStream(fileOutputStream);
                        } catch (IOException e3) {
                            closeable = ˎ;
                            closeable2 = fileOutputStream;
                            FastViewLogUtils.e(DownloadManager.TAG, "IOException");
                            downloadListener.failure(1);
                            IoUtils.closeStream(closeable);
                            IoUtils.closeStream(closeable2);
                        } catch (Throwable th4) {
                            th = th4;
                            IoUtils.closeStream(ˎ);
                            IoUtils.closeStream(fileOutputStream);
                            throw th;
                        }
                    } catch (IOException e4) {
                        closeable = null;
                        FastViewLogUtils.e(DownloadManager.TAG, "IOException");
                        downloadListener.failure(1);
                        IoUtils.closeStream(closeable);
                        IoUtils.closeStream(closeable2);
                    } catch (Throwable th5) {
                        th = th5;
                        ˎ = null;
                        fileOutputStream = null;
                        IoUtils.closeStream(ˎ);
                        IoUtils.closeStream(fileOutputStream);
                        throw th;
                    }
                }
            });
        } catch (IOException e) {
            FastViewLogUtils.e(TAG, "IOException");
            downloadListener.failure(1);
        } catch (NoSuchAlgorithmException e2) {
            FastViewLogUtils.e(TAG, "NoSuchAlgorithmException");
            downloadListener.failure(1);
        } catch (CertificateException e3) {
            FastViewLogUtils.e(TAG, "CertificateException");
            downloadListener.failure(1);
        } catch (KeyStoreException e4) {
            FastViewLogUtils.e(TAG, "KeyStoreException");
            downloadListener.failure(1);
        } catch (UnrecoverableKeyException e5) {
            FastViewLogUtils.e(TAG, "UnrecoverableKeyException");
            downloadListener.failure(1);
        } catch (KeyManagementException e6) {
            FastViewLogUtils.e(TAG, "KeyManagementException");
            downloadListener.failure(1);
        } catch (IllegalAccessException e7) {
            FastViewLogUtils.e(TAG, "IllegalAccessException");
            downloadListener.failure(1);
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public com.huawei.fastengine.fastview.download.download.bean.DownLoadResponse downloadSync(com.huawei.fastengine.fastview.download.download.bean.DownloadRequest r10, android.content.Context r11) {
        /*
        r9 = this;
        r1 = 0;
        r0 = new com.huawei.fastengine.fastview.download.download.bean.DownLoadResponse;
        r0.<init>();
        r2 = r10.getUrl();	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r2 = r9.openConn(r2, r11);	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r3 = r2.getResponseCode();	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r4 = 200; // 0xc8 float:2.8E-43 double:9.9E-322;
        if (r3 == r4) goto L_0x0039;
    L_0x0016:
        r2 = "DownloadManager";
        r4 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r4.<init>();	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r5 = "server response code is not 200,code is ";
        r4 = r4.append(r5);	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r3 = r4.append(r3);	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r3 = r3.toString();	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils.e(r2, r3);	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r2 = 1;
        r0.setErrorCode(r2);	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r1);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r1);
    L_0x0038:
        return r0;
    L_0x0039:
        r3 = r2.getInputStream();	 Catch:{ IOException -> 0x00b0, all -> 0x00c9 }
        r2 = new java.io.File;	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r4 = r11.getFilesDir();	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r5 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r5.<init>();	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r6 = java.lang.System.currentTimeMillis();	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r5 = r5.append(r6);	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r6 = ".apk";
        r5 = r5.append(r6);	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r5 = r5.toString();	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r2.<init>(r4, r5);	 Catch:{ IOException -> 0x00db, all -> 0x00d2 }
        r4 = new java.io.FileOutputStream;	 Catch:{ IOException -> 0x00df, all -> 0x00d2 }
        r4.<init>(r2);	 Catch:{ IOException -> 0x00df, all -> 0x00d2 }
        r1 = 2048; // 0x800 float:2.87E-42 double:1.0118E-320;
        r5 = new byte[r1];	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r1 = r3.read(r5);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
    L_0x006a:
        r6 = -1;
        if (r1 == r6) goto L_0x0076;
    L_0x006d:
        r6 = 0;
        r4.write(r5, r6, r1);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r1 = r3.read(r5);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        goto L_0x006a;
    L_0x0076:
        r1 = r10.isNeedCheck();	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        if (r1 == 0) goto L_0x00a2;
    L_0x007c:
        r1 = r10.getHash();	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r1 = r9.checkRpkValid(r2, r1);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        if (r1 == 0) goto L_0x0094;
    L_0x0086:
        r1 = 0;
        r0.setErrorCode(r1);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r0.setDownloadFile(r2);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r3);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r4);
        goto L_0x0038;
    L_0x0094:
        deleteFile(r2);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r1 = 2;
        r0.setErrorCode(r1);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r3);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r4);
        goto L_0x0038;
    L_0x00a2:
        r1 = 0;
        r0.setErrorCode(r1);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        r0.setDownloadFile(r2);	 Catch:{ IOException -> 0x00e5, all -> 0x00d4 }
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r3);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r4);
        goto L_0x0038;
    L_0x00b0:
        r2 = move-exception;
        r2 = r1;
        r3 = r1;
    L_0x00b3:
        r4 = "DownloadManager";
        r5 = "download error, error is ioexception";
        com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils.e(r4, r5);	 Catch:{ all -> 0x00d7 }
        deleteFile(r1);	 Catch:{ all -> 0x00d7 }
        r1 = 3;
        r0.setErrorCode(r1);	 Catch:{ all -> 0x00d7 }
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r2);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r3);
        goto L_0x0038;
    L_0x00c9:
        r0 = move-exception;
        r3 = r1;
    L_0x00cb:
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r3);
        com.huawei.fastengine.fastview.download.utils.IoUtils.closeStream(r1);
        throw r0;
    L_0x00d2:
        r0 = move-exception;
        goto L_0x00cb;
    L_0x00d4:
        r0 = move-exception;
        r1 = r4;
        goto L_0x00cb;
    L_0x00d7:
        r0 = move-exception;
        r1 = r3;
        r3 = r2;
        goto L_0x00cb;
    L_0x00db:
        r2 = move-exception;
        r2 = r3;
        r3 = r1;
        goto L_0x00b3;
    L_0x00df:
        r4 = move-exception;
        r8 = r2;
        r2 = r3;
        r3 = r1;
        r1 = r8;
        goto L_0x00b3;
    L_0x00e5:
        r1 = move-exception;
        r1 = r2;
        r2 = r3;
        r3 = r4;
        goto L_0x00b3;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.fastengine.fastview.download.download.DownloadManager.downloadSync(com.huawei.fastengine.fastview.download.download.bean.DownloadRequest, android.content.Context):com.huawei.fastengine.fastview.download.download.bean.DownLoadResponse");
    }

    public void resetDownload() {
        FastViewLogUtils.e(TAG, "resetDownload");
        this.isDownload = true;
    }

    public void stopDownload() {
        FastViewLogUtils.e(TAG, "stopDownload");
        this.isDownload = false;
    }
}
